<template>
  <div class="shopcar-container">
    <!-- 商品列表区域 -->
    <div class="goods-list">
      <div class="mui-card" v-for="(item, i) in value" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch v-model="$store.getters.getSelectedObj[item.id]" @change="selectedChange(item.id)"></mt-switch>
            <img :src="item.thumb_path" alt>
            <div class="info">
              <h1>{{ item.title }}</h1>
              <p>
                <span class="price">￥{{ item.sell_price }}</span>
                <numbox :count="$store.getters.getCountById[item.id]" :max="$store.getters.getMaxById[item.id]" :id="item.id"></numbox>
                <a href="javascript:;" @click="deletecar(item.id, i)">删除</a>
              </p>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner go-forBuy">
          <div class="left">
            <p>总计 (不含运费) </p>
            <p>已勾选商品 <span class="red">{{ $store.getters.getAllCount }}</span> 件, 总价<span class="red">￥{{ $store.getters.getAllPrice }}</span></p>
          </div>
          <mt-button type="danger" size="small">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import numbox from "../subcomponents/shopcar_numbox.vue";
import { Toast } from 'mint-ui'
export default {
  data() {
    return {
      value: [],
    };
  },
  methods: {
    getGoodsList() {
      // 1. 获取到car中所有的count值
      let idArr = []
      this.$store.state.car.forEach(item => {
        idArr.push(item.id)
      });
      if(idArr.length <= 0) {
        return;
      }
      // 获取购物车商品列表
      this.$http.get('api/goods/getshopcarlist/' + idArr.join(","))
        .then(result => {
          if (result.body.status === 0) {
            this.value = result.body.message
          } else {
            Toast("获取失败")
          }
        })
    },
    deletecar(id, i) {
      this.$store.commit("deleteCar", id)
      this.value.splice(i, 1)
    },
    selectedChange(id) {
      this.$store.commit("changeSelected", id)
    }
  },
  components: {
    numbox
  },
  created() {
    this.getGoodsList()
  }
};
</script>
style <style lang="scss" scoped>
.shopcar-container {
  background-color: #eee;
  overflow: hidden;
  .mui-card-content-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    
    padding-left: 5px;
    padding-right: 5px;
  }
  img {
    width: 50px;
    height: 50px;
  }
  h1 {
    font-size: 13px;
  }
  .info {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 200px;
    p {
      .price {
        color: red;
      }
      margin: 0;
      display: flex;
      justify-content: space-between;
    }
  }
  .go-forBuy {
    .red{
      color: red;
      font-size: 16px;
      font-weight: boder;
    }
  }
}
</style>

